package com.lzh.controller;

import com.lzh.config.loginConfig.GiteeConfigProperties;
import com.lzh.domain.vo.UserVO;
import com.lzh.service.AuthLoginService;
import com.lzh.utils.ControllerUtils;
import com.lzh.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.request.AuthGiteeRequest;
import me.zhyd.oauth.request.AuthGithubRequest;
import me.zhyd.oauth.request.AuthQqRequest;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


//第三方授权登录
@Slf4j
@RequestMapping("/auth/public")
@RestController
@Api(tags =  "第三方登录接口")
public class AuthLoginController {

    @Resource
    private GiteeConfigProperties giteeConfigProperties;


    @Resource
    private AuthLoginService  authLoginService;

    /**
     * 第三方登录跳转到该页面
     */
    @ApiOperation(value = "第三方登录跳转")
    @PostMapping("/render/{source}")
    public ResponseResult<Object> renderAuth(HttpServletResponse response, @PathVariable String source){
        AuthRequest authRequest = getAuthRequest(source);
        String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());
        return ControllerUtils.responseOkResult(() -> authorizeUrl);
    }

    /**
     * 第三方登录回调
     */

    @ApiOperation(value = "第三方登录回调")
    @GetMapping("/callback/{source}")
    public void callBackAuth(AuthCallback callback, HttpServletResponse response, @PathVariable String source) throws IOException {
        AuthRequest authRequest = getAuthRequest(source);
        AuthResponse authResponse = authRequest.login(callback);
        authLoginService.login(response,source,authResponse,callback.getState());
    }


    @ApiOperation(value = "根据token获取用户信息")
    @GetMapping("/getUserByToken")
    public ResponseResult<UserVO> getUserInfo(@RequestParam String token){
        return authLoginService.getUserInfo(token);
    }


    /**
     * 获取第三方登录请求
     * @param source
     * @return
     */
    private AuthRequest getAuthRequest(String source){
        AuthRequest authRequest = null;
        switch (source){
            case "gitee":
                authRequest = new AuthGiteeRequest(AuthConfig
                        .builder()
                        .clientId(giteeConfigProperties.getAppId())
                        .clientSecret(giteeConfigProperties.getAppSecret())
                        .redirectUri(giteeConfigProperties.getRedirectUrl())
                        .build()
                );
                break;
             case "github":
                 authRequest = new AuthGithubRequest(AuthConfig
                         .builder()
                         .clientId(giteeConfigProperties.getAppId())
                         .clientSecret(giteeConfigProperties.getAppSecret())
                         .redirectUri(giteeConfigProperties.getRedirectUrl())
                         .build()
                 );
                 break;
            case "qq":
                authRequest = new AuthQqRequest(AuthConfig
                        .builder()
                        .clientId(giteeConfigProperties.getAppId())
                        .clientSecret(giteeConfigProperties.getAppSecret())
                        .redirectUri(giteeConfigProperties.getRedirectUrl())
                        .build()
                );
                break;
            default:
                break;
        }
        return authRequest;
    }




}
